<?php

namespace app\admin\controller;

use think\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Worktype extends AdminControl {

    const EXPORT_SIZE = 1000;

    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/worktype.lang.php');
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/zhuwork.lang.php');
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/admin.lang.php');
    }

    /**
     * 子客服类型管理
     */
    public function index() {
        $model = model('worktype');
        $condition=[];
        /**
         * 检索条件
         */
        if (!empty(input('param.search_type_name'))) {
            $condition['ds_type_work.name'] = array('like', "%" . input('param.search_type_name') . "%");
        }
        $list = $model->getWorktypeList($condition, "ds_type_work.*,g.gname", 10);
        $this->assign('showpage', $model->page_info->render());
        $this->assign('list', $list);
       $this->assign('search_type_name', trim(input('param.search_type_name')));
        $this->setAdminCurItem('index');
        return $this->fetch();
    }

    /**
     * 增加子客服类型
     */
    public function worktype_add() {
        $model = model('worktype');
        if (request()->isPost())
        {
           $type_validate = validate('worktype');
           if (!$type_validate->scene('type_add')->check($_POST))
           {
               $this->error($type_validate->getError());
           }
            $_POST['created_at'] = $_POST['updated_at'] = date('Y-m-d H:i:s',time());
            $_POST['admin_id'] = session('admin_id');
            $_POST['admin_name'] = session('admin_name');
           if ($model->addWorktype($_POST))
           {
               $this->log(lang('ds_add') . lang('worktype_index_zhuwork') . '[' . input('post.name') . ']', 1);
               dsLayerOpenSuccess(lang('ds_common_save_succ'));
           }else{
               $this->error(lang('ds_common_save_fail'));
           }
        }else{
            //得到权限组
            $admin_model = model('admin');
            $gadmin = $admin_model->getGadminList2('gname,gid',[]);
            $arr = [
                'id'=>'',
                'name'=>'',
                'sort'=>1,
                'status'=>1,
                'gid'=>''
            ];
            $this->assign('gadmin', $gadmin);
            $this->assign('array',$arr);
            return $this->fetch('form');
        }
    }

    /**
     * 修改子客服类型
     */
    public function worktype_edit()
    {
        $model = model('worktype');
        if (request()->isPost())
        {
            $type_validate = validate('worktype');
            if (!$type_validate->scene('type_edit')->check($_POST))
            {
                $this->error($type_validate->getError());
            }
            $_POST['updated_at'] = date('Y-m-d H:i:s',time());
            $res = $model->editWorktype(['id'=>input('post.id')],$_POST);
            if ($res)
            {
                $this->log(lang('ds_update') . lang('worktype_index_zhuwork') . '[' . input('post.name') . ']', 1);
                dsLayerOpenSuccess(lang('ds_common_save_succ'));
            }else{
                $this->log(lang('ds_update') . lang('worktype_index_zhuwork') . '[' . input('post.name') . ']', 0);
                $this->error(lang('ds_common_save_fail'));
            }
        }else{
            //得到权限组
            $admin_model = model('admin');
            $gadmin = $admin_model->getGadminList2('gname,gid',[]);
            $res = $model->getworktypeInfo(['id'=>input('get.type_id')]);
            $this->assign('array',$res);
            $this->assign('gadmin', $gadmin);
            return $this->fetch('form');
        }
    }

    /**
     * 删除子客服类型
     */
    public function del_worktype()
    {
        $model = model('worktype');
        $typeid = input('param.type_id');
        $typeid_arr = ds_delete_param($typeid);
        if ($typeid_arr == FALSE)
        {
            $this->log(lang('ds_del') . lang('worktype_index_zhuwork') . '[ID:' . $typeid . ']',0);
            ds_json_encode(10001,lang('param_error'));
        }
        $model->delWorktype(array('id'=>array('in',implode(',',$typeid_arr))));
        $this->log(lang('ds_del') . lang('worktype_index_zhuwork') . '[ID:' . $typeid . ']', 1);
        ds_json_encode(10000, lang('ds_common_del_succ'));
    }

    /**
     * ajax
     */
    public function ajax()
    {
        $model = model('worktype');
        switch (input('get.branch'))
        {
            case 'check_type_name':
                $condition['name'] = input('get.typename');
                $condition['id']=['neq',input('get.typeid')];
                $info =$model->getworktypeInfo($condition);
                if ($info)
                {
                    exit('false');
                }else{
                    exit('true');
                }

        }
    }

    public function edit_status()
    {
        $model = model('worktype');
        $condition['id']= input('param.id');
        $data[input('param.column')] = input('param.value');
        $model->editWorktype($condition,$data);
        $this->log(lang('ds_edit') . lang('worktype_index_zhuwork') . '[ID:' . intval(input('param.id')) . ')', 1);
        ds_json_encode(10000, lang('ds_common_op_succ'));
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'index',
                'text' => lang('ds_manage'),
                'url' => url('Worktype/index'),
            ),
            // array(
            //     'name' => 'brand_add',
            //     'text' => lang('ds_add'),
            //     'url' => "javascript:dsLayerOpen('".url('Worktype/worktype_add')."','".lang('ds_add')."')"
            // ),
        );
        return $menu_array;
    }
}